package com.dnjn.happiness.emo.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.utils.DateUtils;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.domain.EmoAvgScoreWeekly;
import com.dnjn.happiness.emo.api.domain.WeeklyRankingBean;
import com.dnjn.happiness.emo.domain.vo.EmoAvgScoreWeeklyVO;
import com.dnjn.happiness.emo.domain.vo.WeeklyComparisonVO;
import com.dnjn.happiness.emo.service.IEmoAvgScoreWeeklyService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 快乐值周均Controller
 *
 * @author happiness
 * @date 2024-02-27
 */
@Tag(name = "score admin weekly 快乐值周均", description = "快乐值周均")
@RestController
@RequestMapping("/admin/score/weekly")
public class AdminEmoAvgScoreWeeklyController extends BaseController {
    @Autowired
    private IEmoAvgScoreWeeklyService emoAvgScoreWeeklyService;

    /**
     * 查询快乐值周均列表
     */
    @RequiresPermissions("@ss.hasPermi('score:weekly:list')")
    @PostMapping("/list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody EmoAvgScoreWeeklyVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        EmoAvgScoreWeekly emoAvgScoreWeekly = new EmoAvgScoreWeekly();
        BeanUtils.copyPropsIgnoreNull(vo, emoAvgScoreWeekly);
        Page page = Page.of(current, size);
        Page<EmoAvgScoreWeekly> list = emoAvgScoreWeeklyService.page(page, Wrappers.query(emoAvgScoreWeekly));
        return success(list);
    }

    /**
     * 导出快乐值周报表 均列表
     */
    @Log(title = "快乐值周均", businessType = BusinessType.EXPORT)
    @RequiresPermissions("@ss.hasPermi('score:weekly:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmoAvgScoreWeeklyVO vo) {
        EmoAvgScoreWeekly emoAvgScoreWeekly = new EmoAvgScoreWeekly();
        BeanUtils.copyPropsIgnoreNull(vo, emoAvgScoreWeekly);
        if (Objects.isNull(vo.getDeptId()) || vo.getDeptId().equals(0L)) {
            emoAvgScoreWeekly.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        }

        List<EmoAvgScoreWeekly> list = emoAvgScoreWeeklyService.getExportList(emoAvgScoreWeekly);
        emoAvgScoreWeeklyService.exportExcel(response, vo.getDeptId());

    }

    /**
     * 导出快乐值周对比报表
     */
    @Log(title = "快乐值周均", businessType = BusinessType.EXPORT)
    @RequiresPermissions("@ss.hasPermi('score:weekly:export')")
    @PostMapping("/exportWeeklyComparison")
    public void exportWeeklyComparison(HttpServletResponse response, EmoAvgScoreWeeklyVO vo) {
        EmoAvgScoreWeekly emoAvgScoreWeekly = new EmoAvgScoreWeekly();
        BeanUtils.copyPropsIgnoreNull(vo, emoAvgScoreWeekly);

        List<EmoAvgScoreWeekly> list = emoAvgScoreWeeklyService.list(Wrappers.query(emoAvgScoreWeekly));
        emoAvgScoreWeeklyService.exportWeeklyComparison(response);

    }

    /**
     * 获取快乐值周均详细信息
     */
    @RequiresPermissions("@ss.hasPermi('score:weekly:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(emoAvgScoreWeeklyService.getById(id));
    }

    /**
     * 新增快乐值周均
     */
    @Log(title = "快乐值周均", businessType = BusinessType.INSERT)
    @RequiresPermissions("@ss.hasPermi('score:weekly:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EmoAvgScoreWeeklyVO vo) {
        EmoAvgScoreWeekly emoAvgScoreWeekly = new EmoAvgScoreWeekly();
        BeanUtils.copyPropsIgnoreNull(vo, emoAvgScoreWeekly);

        emoAvgScoreWeekly.setCreateBy(SecurityUtils.getUsername());
        return toAjax(emoAvgScoreWeeklyService.save(emoAvgScoreWeekly));
    }

    /**
     * 修改快乐值周均
     */
    @Log(title = "快乐值周均", businessType = BusinessType.UPDATE)
    @RequiresPermissions("@ss.hasPermi('score:weekly:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody EmoAvgScoreWeeklyVO vo) {
        EmoAvgScoreWeekly emoAvgScoreWeekly = new EmoAvgScoreWeekly();
        BeanUtils.copyPropsIgnoreNull(vo, emoAvgScoreWeekly);
        return toAjax(emoAvgScoreWeeklyService.updateById(emoAvgScoreWeekly));
    }

    /**
     * 删除快乐值周均
     */
    @Log(title = "快乐值周均", businessType = BusinessType.DELETE)
    @RequiresPermissions("@ss.hasPermi('score:weekly:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(emoAvgScoreWeeklyService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 统计分析 - 周日历折线图
     */
    @GetMapping("/getWeeklyComparison")
    @RequiresPermissions("@ss.hasPermi('score:weekly:query')")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getWeeklyComparison(@RequestParam(required = false) Long deptId) {
        //查询当前年 周均快乐值
        if (Objects.isNull(deptId) || deptId.equals(0L)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        List<WeeklyComparisonVO> voList = emoAvgScoreWeeklyService.getWeeklyComparison(deptId);

        return AjaxResult.success(voList);
    }

    /**
     * 统计分析 - 周日历导出所选周
     */
    @PostMapping("/exportWeeklyCalendar")
    @RequiresPermissions("@ss.hasPermi('score:weekly:export')")
    public void exportWeeklyCalendar(HttpServletResponse response, @RequestBody List<WeeklyComparisonVO> voList, @RequestParam(required = false) Long deptId) {
        if (Objects.isNull(deptId) || deptId.equals(0L)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        emoAvgScoreWeeklyService.exportWeeklyCalendar(response, voList, deptId);
    }

    /**
     * 快乐值比较1周度
     */
    @RequiresPermissions("@ss.hasPermi('score:weekly:query')")
    @GetMapping("/getCompareWeekHappinessScore")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getCompareWeekHappinessScore(Long deptId) {
        if (Objects.isNull(deptId) || deptId.equals(0L)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        return emoAvgScoreWeeklyService.getCompareWeekHappinessScore(deptId);
    }


    /**
     * 快乐值比较前十名，后十名，波动前十名展示
     */
    @RequiresPermissions("@ss.hasPermi('score:weekly:query')")
    @GetMapping("/getWeekTenPlaces")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getWeekTenPlaces(Long deptId) {
        if (Objects.isNull(deptId) || deptId.equals(0L)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        //查询当前周
        int week = DateUtils.getWeek();
        //当前年数
        int year = DateUtils.getYear();
        String weekOfYear = String.format("%04d%02d", year, week);
        List<WeeklyRankingBean> agoList = emoAvgScoreWeeklyService.getTopTen(weekOfYear, deptId);
        List<WeeklyRankingBean> afterList = emoAvgScoreWeeklyService.getLastTen(weekOfYear, deptId);
        List<EmoAvgScoreWeekly> waveList = emoAvgScoreWeeklyService.getLargeFluctuate(weekOfYear, deptId);
        return new AjaxResult().put("agoList", agoList).put("afterList", afterList).put("waveList", waveList);
    }


}
